﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ExceptionExtensions.cs" company="Matthias Friedrich">
//   Copyright © Matthias Friedrich 2013
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace CommandlineHandling
{
    using System;
    using System.Linq.Expressions;

    public static class ExceptionExtensions
    {
        public static void ThrowArgumentNullException<T>(T value, Expression<Func<T>> argumentExpression)
        {
            if (value == null)
            {
                var memberExpr = (MemberExpression)argumentExpression.Body;

                throw new ArgumentNullException(memberExpr.Member.Name);
            }
        }

        public static void ThrowArgumentNullException(string value, Expression<Func<string>> argumentExpression, bool allowEmpty = false)
        {
            if (string.IsNullOrEmpty(value) && !allowEmpty)
            {
                var memberExpr = (MemberExpression)argumentExpression.Body;

                throw new ArgumentNullException(memberExpr.Member.Name);
            }
        }
    }
}